<template>
  <div class="containe-comment">
    <el-card>
      <div slot="header">
        <my-bread>评论管理</my-bread>
      </div>
      <!-- 表格 -->
      <el-table :data="comments">
        <el-table-column label="文章便签" prop="title"></el-table-column>
        <el-table-column label="总评论数" prop="total_comment_count"></el-table-column>
        <el-table-column label="粉丝评论数" prop="fans_comment_count"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">{{scope.row.comment_status?'已打开':'已关闭'}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" width="100px">
            <el-button
              size="small"
              v-if="scope.row.comment_status"
              @click="toggleStatus(scope.row)"
              type="danger"
            >关闭评论</el-button>
            <el-button v-else type="success" size="small" @click="toggleStatus(scope.row)">打开评论</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px"
        :total="total"
        :page-size="reqParams.per_page"
        :current-page="reqParams.page"
        :page-sizes="[10,20,30,50]"
        @current-change="changePager"
        @size-change="changeSize"
        background
        layout="prev, pager, next,sizes"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "comment",
  data() {
    return {
      comments: [],
      reqParams: {
        page: 1,
        per_page: 10,
        //获取评论的相关数据
        response_type: "comment",
      },
      total: 0,
    };
  },
  created() {
    this.getcomments();
  },
  methods: {
    //打开评论 关闭评论
    toggleStatus(row) {
      //row 中有id文章中的ID comment_status 当前的平论状态
      const text1 =
        "此操作会关闭该文章的评论功能，关闭后用户将无法对该文章进行评论，您确认吗？";
      const text2 = "此操作会开启该文章的评论功能，您确认吗？";
      //判断评论的的状态,
      const text = row.comment_status ? text1 : text2;
      //确认框 提示
      this.$confirm(text, "温馨提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //点击确认
          // await this.$http.put(`comment/status?article_id=${row.id}`, {
          //   allow_comment: !row.comment_status,
          // });
          try {
            await this.$http({
              method: "put",
              url: "comments/status",
              params: {
                //row.id超出了最大安全数值.json-bigint转换成了对象,自己转换成字符串
                article_id: row.id.toString(),
              },
              //请求体传参
              data: {
                allow_comment: !row.comment_status,
              },
            });
            this.$message.success(row.comment_status ? "关闭成功" : "打开成功");
            //修改row当前状态,驱动当前进行渲染
            row.comment_status = !row.comment_status;
          } catch (e) {
            this.$message.error(row.comment_status ? "关闭失败" : "打开失败");
          }
        })
        .catch(() => {});
    },
    // 改变尺码
    changeSize(size) {
      this.reqParams.page = 1;

      this.reqParams.per_page = size;
      this.getcomments();
    },
    //进行分页
    changePager(newPage) {
      this.reqParams.page = newPage;
      this.getcomments();
    },
    //获取评论数据
    async getcomments() {
      const res = await this.$http.get("articles", { params: this.reqParams });
      this.comments = res.data.data.results;
    },
  },
};
</script>

<style>
</style>